package com.fishery.monitor.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.monitor.dao.MeteorologicalDataDao;
import com.fishery.monitor.mapper.MonitorMeteorologicalDataMapper;
import com.fishery.monitor.pojo.Equipment;
import com.fishery.monitor.pojo.MeteorologicalData;
import com.fishery.monitor.pojo.MonitorBase;
import com.fishery.monitor.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层
 *
 * @author Administrator
 */
@Service
public class MeteorologicalDataService extends ServiceImpl<MonitorMeteorologicalDataMapper, MeteorologicalData> {

    @Autowired
    private MeteorologicalDataDao meteorologicalDataDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<MeteorologicalData> findAll() {
        return this.list(null);
    }




    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public IPage<MeteorologicalData> findSearch(Map whereMap, int page, int size) {
//        Specification<MeteorologicalData> specification = createSpecification(whereMap);
//        Sort sort=new Sort(Sort.Direction.ASC,"acquisitionTime");
//        PageRequest pageRequest = PageRequest.of(page - 1, size,sort);
        Page<MeteorologicalData> Page = new Page<>(page,size);
        QueryWrapper<MeteorologicalData> wrapper = new QueryWrapper<>();
        HashMap<String, String> searchMap = new HashMap();
        searchMap.putAll(whereMap);
        searchMap.remove("checkItemName");
        searchMap.remove("typeId");
        searchMap.remove("startTime");
        searchMap.remove("endTime");
        searchMap.remove("equipmentId");
        wrapper.orderByDesc(new String[]{"acquisitionTime"});
        wrapper.allEq(searchMap, false);
        return meteorologicalDataDao.findAll(Page, wrapper);
    }
//    /**
//     * 预测需要的源数据
//     *
//     * @param whereMap
//     * @param page
//     * @param size
//     * @return
//     */
//    public Page<Double> querySource(Map whereMap, int page, int size) {
//        PageRequest pageRequest = PageRequest.of(page - 1, size);
//        return meteorologicalDataDao.querySource((String)whereMap.get("checkItemName"), (String)whereMap.get("equipmentId"),(String)whereMap.get("startTime"),(String)whereMap.get("endTime"),pageRequest);
//    }

    public IPage<MeteorologicalData> querySource(Map whereMap, int page, int size) {
//        Specification<MeteorologicalData> specification = createSpecification(whereMap);
//        Sort sort=new Sort(Sort.Direction.ASC,"acquisitionTime");
//        PageRequest pageRequest = PageRequest.of(page - 1, size,sort);
        Page<MeteorologicalData> Page = new Page<>(page,size);
        QueryWrapper<MeteorologicalData> wrapper = new QueryWrapper<>();
        HashMap<String, String> searchMap = new HashMap();
        if (whereMap.get("startTime") != null && !"".equals(whereMap.get("startTime"))) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                wrapper.ge("acquisitionTime", dateFormat.parse((String)whereMap.get("startTime")));
                if (whereMap.get("endTime") != null && !"".equals(whereMap.get("endTime"))) {
                    wrapper.le("acquisitionTime", dateFormat.parse((String)whereMap.get("endTime")));
                }
            } catch (ParseException var5) {
                var5.printStackTrace();
            }
        }
        searchMap.putAll(whereMap);
        searchMap.remove("checkItemName");
        searchMap.remove("typeId");
        searchMap.remove("startTime");
        searchMap.remove("endTime");
//        searchMap.remove("equipmentId");
        wrapper.orderByDesc(new String[]{"acquisitionTime"});
        wrapper.allEq(searchMap, false);
        return meteorologicalDataDao.findAll(Page, wrapper);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<MeteorologicalData> findSearch(Map whereMap) {
        QueryWrapper<MeteorologicalData> wrapper = new QueryWrapper<>();
        HashMap<String, String> searchMap = new HashMap();
        searchMap.putAll(whereMap);
        if (whereMap.get("startTime") != null && !"".equals(whereMap.get("startTime"))) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                wrapper.ge("acquisitionTime", dateFormat.parse((String)whereMap.get("startTime")));
                if (whereMap.get("endTime") != null && !"".equals(whereMap.get("endTime"))) {
                    wrapper.le("acquisitionTime", dateFormat.parse((String)whereMap.get("endTime")));
                }
            } catch (ParseException var5) {
                var5.printStackTrace();
            }
        }

        searchMap.remove("checkItemName");
        searchMap.remove("typeId");
        searchMap.remove("startTime");
        searchMap.remove("endTime");
        wrapper.orderByDesc(new String[]{"acquisitionTime"});
        wrapper.allEq(searchMap, false);
        return meteorologicalDataDao.findAll(wrapper);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public MeteorologicalData findById(String id) {
        return this.findById(id);
    }

    /**
     * 增加
     *
     * @param meteorologicalData
     */
    public void add(MeteorologicalData meteorologicalData) {
        meteorologicalData.setId(idWorker.nextId() + "");
        this.save(meteorologicalData);
    }

    /**
     * 批量增加
     *
     * @param
     */
    public void addList(List<MeteorologicalData> ls) {
        if (ls == null || ls.size() == 0) {
            return;
        }
        for (MeteorologicalData meteorologicalData : ls) {

            this.save(meteorologicalData);
        }
    }


    /**
     * 修改
     *
     * @param meteorologicalData
     */
    public void update(MeteorologicalData meteorologicalData) {
        this.save(meteorologicalData);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        this.removeById(id);
    }





}
